package lock;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileLock;

import org.apache.log4j.Logger;

public class Loock {
	
	public static final Logger logger = Logger.getLogger(Loock.class); // Log
	
	public void main() {
		logger.info("Starting");
		
		File fl = new File("lock");
		try {
			logger.info("Trying to open file");
			RandomAccessFile file = new RandomAccessFile(fl, "rw");
			logger.info("File is open");
			FileLock lock = file.getChannel().tryLock();
			logger.info("Lock status: "+ (lock!=null ? lock.isValid():"null"));
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error(e);
			e.printStackTrace();
		}
		

		
		logger.info("Wait for input...");
		try {
			System.in.read();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			logger.error(e);
		}
		
		
		
		
//		 FileLock fileLock = ((Object) fl).getChannel().tryLock();
	}
}
